package com.yj.web.WeChat.handler;

import com.yj.web.WeChat.Message.WxXmlMessage;
import com.yj.web.WeChat.Message.WxXmlOutMessage;
import com.yj.web.WeChat.Message.WxXmlOutNewsMessage;
import com.yj.web.WeChat.api.WxConfigStorage;
import com.yj.web.WeChat.api.WxService;
import com.yj.web.WeChat.service.WxCoreService;
import com.yj.web.dao.mapper.IntroducerMapper;
import com.yj.web.dao.po.admin.Introducer;
import com.yj.web.util.Session.WxSessionManager;
import com.yj.web.util.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by yanlinyang on 2017/9/11.
 */
@Component
public class SubscribeHandler extends AbstractHandler {
    @Autowired
    protected WxConfigStorage configStorage;
    @Autowired
    protected WxService wxService;
    @Autowired
    protected WxCoreService coreService;
    @Autowired
    private IntroducerMapper introducerMapper;

    @Override
    public WxXmlOutMessage handle(WxXmlMessage wxMessage, Map<String, Object> context, WxService wxService, WxSessionManager sessionManager) throws WxErrorException {
        String itemPhone = null;
        String introducerId = wxMessage.getEventKey();
        if (!"".equals(introducerId)) {
            introducerId = introducerId.replace("qrscene_", "");
            final String phone;
            if(!introducerId.contains("tel")){
                Introducer introducer = introducerMapper.findIntroducerById(Integer.parseInt(introducerId));
                phone = introducer.getPhone();
            } else {
                phone = introducerId.replaceAll("tel","");
            }
            itemPhone = phone;
            ExecutorService service = Executors.newFixedThreadPool(1);
            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    String openId = wxMessage.getFromUser();
                    Set<String> openIds = introducerMapper.getAllOpenIds();
                    if (openIds.contains(openId)) {
                        introducerMapper.updateFans(openId, phone);
                    } else {
                        introducerMapper.insertFans(openId, phone);
                    }
                }
            });
            service.submit(thread);
        }

        WxXmlOutNewsMessage.Item item = new WxXmlOutNewsMessage.Item();
        item.setDescription("");
        item.setTitle("用医诺电子借条，向朋友借钱看病");
        item.setPicUrl("http://mmbiz.qpic.cn/mmbiz_jpg/UfLVmicNJTDibW4PIaMrlk5nbtcu7iaibUiaTytSqwKfTfibFvicekI8TP13fI59LorG3iaGTKmNEBKWicVeia8rlHf3SxDw/0");
        if (null != itemPhone) {
            item.setUrl("http://m.yinuochoukuan.com/register?beInviteCode=" + itemPhone);
        } else {
            item.setUrl("http://m.yinuochoukuan.com/register");
        }
        WxXmlOutNewsMessage m
                = WxXmlOutMessage.NEWS()
                .addArticle(item)
                .fromUser(wxMessage.getToUser())
                .toUser(wxMessage.getFromUser())
                .build();
        logger.info("subscribeHandler" + m.getMsgType());
        return m;

//        WxXmlOutTextMessage m
//                = WxXmlOutMessage.TEXT()
//                .content("尊敬的" + wxUser.getNickname() + "，您好！")
//                .fromUser(wxMessage.getToUser())
//                .toUser(wxMessage.getFromUser())
//                .build();
//        logger.info("subscribeMessageHandler" + m.getContent());
//        return m;
    }
}
